New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ac-sanitizer

Package Overview
Dependencies
Maintainers
0
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ac-sanitizer

Sanitites payloads based on given field definitions

  • 4.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

AC Sanitizer

Sanitizes payloads with given field definitions

Node.js CI

Version 4 - Breaking changes

Version 4 requires Node 16.

Usage

const sanitizer = require('ac-sanitizer')

let fieldsToCheck = {
  params: {
    stringVar: 'This is a string'
  },
  fields: [
    { field: 'stringVar', type: 'string', required: true },
    { field: 'forceJob', type: 'boolean', adminLevel: 64 }
  ],
  adminLevel: 32 // optional adminLevel of the user - must be at least the one defined in fields
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)
// COMPLEX EXAMPLE WITH NESTED PROPERTIES AND CONDITIONAL REQUIREMENTS

const sanitizer = require('ac-sanitizer')

let fieldsToCheck = {
  params: {
    obj: {
      f1: true
  },
  fields: [
    { field: 'obj', type: 'object', properties: [
      { field: 'f1', type: 'boolean' },
      { field: 'f2', type: 'boolean', required: 'f1' } // if f1 is true, then f2 is required
    ]}
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)

Field definition

ParameterTypeRemarks
fieldstringName of the field
typestringType of the field to sanitize, see below for available values
required[boolean OR string]Set to true if required or set a path1 to a param (if that param is set, this value is required)
enum[array OR string]Optional list of allowed values. You can a string placeholder for certain standard lists (see below)
adminLevel[integer]Optional adminLevel required for this field
omitFields[boolean]If adminLevel is set and you do not have the proper adminLevel the sanitizer will just omit the field (and not return an error) if omitFields is true
convert[boolean OR string]Some types can be automatically converted (e.g. base64 to string)
valueType[string]Use it to sanitize values of an array by defining the allowed type here
strict[boolean]For objects only - if true and payload contains a property not defined, an error will be returned.
nullAllowed[boolean]If true, sending NULL is allowed.

ENUM lists

The following enum lists are available using a string placeholder

PlaceholderItemsRemarks
iso-639-1ISO 639-1 entriese.g. de, en, fr, es...
iso-639-2ISO 639-2 entriese.g. deu, eng, fra ...
countrylistlist of country namese.g. Laos, Brazil, Norway...

Convert

Some types allow automatic conversion:

TypeExampleRemarks
integer60.1 -> 60Convert incoming number to integer - this way you can make your check more lenient
stringHello Developer -> Hello (with maxLength = 5)Reduce string to max length
base64SGVsbG8= -> HelloConvert base64 encoded string to UTF-8 string
iso-639{ iso-639-2: 'tlh', translations: [] } -> tlh (with convert=iso-639-2)Returns only the select property for the ISO-639 object

Available types

TypeOptionsRemarks
anyAny can be string, integer, boolean, object or array - it will be automatically detected.
base64Checks if a string is base64 encoded, optional with field option "convert" (to string)
boolean
cidrCheck CIDR, see example
integer | booleanValue can be an integer OR a boolean
datedateFormatDate or date time, support various date formats (e.g. DD.MM.YYYY, DD/MM/YYYY, YYYY-MM-DD, etc) as well as a custom format defined in dateFormat.
fqdnwildcardAllowed (bool)Fully qualified domain names, optional with wildcard subdomain (e.g. *.admiralcloud.com)
emaila@b.c
float0 - 2^31
fileExtension
gpsCan be a string of "LAT,LNG" or one including a distance as 3rd parameter like "LAT,LNG,DISTANCE".
hashidsHashIds - https://hashids.org
hexColorCheck valid hexadecimal color like #ff99cc
integer0 - 2^31
integer | stringValue can be an integer OR a string
iso-639-1convertWith convert = nativeName you can retrieve the native name of the given ISO string
iso-639-2convertWith convert = nativeName you can retrieve the native name of the given ISO string
ipversionversion can be "4" or "6", defaults to "4"
long0 - 2^63
objectpropertiesUse properties to define object structure, properties is equal to fields array
numberShould no be used - use integer, long, short, floag
ratiox:y
rgbCheck for valid RGB value (r,g,b) or (r%,g%, b%)
short0 - 2^15
stringminLength (int), maxLength (int), ignoreCaseWith ignoreCase=true enum checks are case insensitive (e.g. abc in ['ABC'] is valid)
urlprotocols, require_tld, require_protocolDefault values: protocols ['http', 'https'], required_tld true, require_protocol true

Examples

CIDR

// CIDR Example

const sanitizer = require('ac-sanitizer')

let fieldsToCheck = {
  params: {
    cidr: [{ cidr: '8.8.8.8/32' }]
  },
  fields: [
    { field: 'cidr', type: 'cidr' }
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)


// multiple mixed CIDR
let fieldsToCheck = {
  params: {
    cidr: [
      { cidr: '8.8.8.8/32' },
      { cidr: '::ffff:127.0.0.1', type: 'ipv6' }
    ]
  },
  fields: [
    { field: 'cidr', type: 'cidr' }
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)

Objects

By default properties which are not defined will be ignored and removed from payload.

Objects with strict mode activate

In strict mode, only defined properties are allowed.

let fieldsToCheck = {
  params: {
    settings: {
      allowed: true,
      notAllowed: true
    }
  },
  fields: [
    { field: 'settings', type: 'object', strict: true, properties: [
      { field: 'allowed', type: 'boolean' }
    ] }
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)
// error: object_settings_containsInvalidProperties

  • Website

License

MIT License Copyright © 2009-present, AdmiralCloud AG, Mark Poepping

Footnotes

  1. The path must be set with the parent propery as root, e.g. the actual field is settings.video.width, in property video the condition is then just "width" not the full path.

FAQs

Package last updated on 25 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc